import { Request, Response } from "express";
import { NextFunction } from "express-serve-static-core";
const express = require("express");
const router = express.Router();
let rp = require("request-promise");
import jar from "../toughCookie";
import { json } from "body-parser";
import r from "../r";

import * as config from "../../config";

router.get("/selectHistory", async function(
  req: Request,
  res: Response,
  next: NextFunction
) {
  const { email, name, phone, uid, p, size } = req.query;
  const data: Array<any> = [
    {
      code: "1",
      uid: "0.00002552",
      phone: "13370277157",
      email: "986177191@qq.com",
      symbol: "BTC/USD",
      amount: "0.01",
      aveSuccessPrice: "0.03",
      passFee: "5",
      successAmount: "555",
      createTime: 1542858853,
      updateTime: 1542858853,
      type: "2",
      lists: [
        {
          createTime: 1542858853,
          price: "333.333",
          amount: "333.222",
          passFee: "3333.222"
        }
      ]
    },
    {
      code: "2",
      uid: "324234234",
      phone: "13370277157",
      email: "986177191@qq.com",
      symbol: "BTC/USD",
      amount: "0.01",
      aveSuccessPrice: "0.03",
      passFee: "5",
      successAmount: "555",
      createTime: 1542858853,
      updateTime: 1542858853,
      type: "3",
      lists: [
        {
          createTime: 1542858853,
          price: "333.333",
          amount: "333.222",
          passFee: "3333.222"
        }
      ]
    }
  ]
    .filter(d => {
      if (!!email) {
        return d.email.includes(email);
      }
      return true;
    })
    .filter(d => {
      if (!!phone) {
        return d.phone.includes(phone);
      }
      return true;
    })
    .filter(d => {
      if (!!uid) {
        return d.uid.includes(uid);
      }
      return true;
    });
  if (process.env.NODE_ENV === "eh") {
    res.json({
      code: 200,
      data,
      msg: "success"
    });
  }
  try {
    const newurl = `${config.beApi}/admin/order/history`;

    const ret = await r(
      {
        url: newurl,
        body: JSON.stringify({
          pageNo: p,
          pageSize: size
        })
      },
      req,
      res
    );
    const jsonRet = JSON.parse(ret.body);

    if (
      !!jsonRet &&
      !!jsonRet.data &&
      !!jsonRet.data.data &&
      !!jsonRet.data.data.list
    ) {
      jsonRet.total = jsonRet.data.total;
      jsonRet.data = jsonRet.data.data.list;
      jsonRet.data = jsonRet.data.slice().map((d: any) => {
        return {
          code: "1",
          uid: "0.00002552",
          phone: "13370277157",
          email: "986177191@qq.com",
          symbol: "BTC/USD",
          amount: "0.01",
          aveSuccessPrice: "0.03",
          passFee: "5",
          successAmount: "555",
          createTime: 1542858853,
          updateTime: 1542858853,
          type: "2",
          lists: [
            {
              createTime: 1542858853,
              price: "333.333",
              amount: "333.222",
              passFee: "3333.222"
            }
          ]
        };
      });
    } else {
      jsonRet.data = [];
      jsonRet.total = 0;
    }
    res.json(jsonRet);
  } catch (e) {
    res.json({
      code: -1,
      data: undefined,
      msg: e
    });
  }
});

export default router;
